<template>
  <div>
    <!--     搜索条件-->
    <SearchForm @search="getTableData" @reset="resetPage">
      <el-col :span="8" :xs="24" v-show="$permiss[171]">
        <el-form-item :label="$t('所属租户')" prop="adminUserId">
          <el-select style="width: 100%" @change="tenantChange" v-model="page.adminUserId" clearable>
            <el-option v-for="item in tenantList" :key="item.adminUserId"
                       :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('用户姓名')" prop="name">
          <el-input v-model="page.name" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('手机号码')" prop="phone">
          <el-input v-model="page.phone" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('身份证号码')" label-width="100px">
          <el-input v-model="page.idcard" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('邮箱')">
          <el-input v-model="page.email" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('注册渠道')" prop="da">
          <el-select v-model="page.da" style="width: 100%">
            <el-option :value="1" label="1"></el-option>
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="8" :xs="24" v-show="$permiss[172]">
        <el-form-item :label="$t('APP名称')" prop="productName">
          <el-select style="width: 100%" v-model="page.productName" clearable>
            <el-option v-for="item in productList" :key="item.productId"
                       :value="item.productName"
                       :label="item.productName"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('结清次数')" prop="settlementNum">
          <el-input v-model.number="page.settlementNum" class="input-with-select">
            <el-select style="width: 60px" v-model="page.settlementSymbol"
                       slot="prepend">
              <el-option v-for="item in symbolList" :key="item.label" :label="item.label"
                         :value="item.label"></el-option>
            </el-select>
            <el-button slot="append">{{ $t('次') }}</el-button>
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('正常还款率')" label-width="100px" prop="normalRefundRatio">
          <el-input v-model.number="page.normalRefundRatio" class="input-with-select">
            <el-select style="width: 60px" v-model="page.normalRefundSymbol"
                       slot="prepend">
              <el-option v-for="item in symbolList" :key="item.label" :label="item.label"
                         :value="item.label"></el-option>
            </el-select>
            <el-button slot="append">%</el-button>
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('免审状态')" prop="avoidAudit">
          <el-select v-model="page.avoidAudit" style="width: 100%">
            <el-option :label="$t('是')" :value="1"></el-option>
            <el-option :label="$t('否')" :value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" :xs="24">
        <el-form-item :label="$t('注册时间')" prop="timeValue">
          <el-date-picker
              v-model="page.timeValue"
              style="width: 100%"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="~"
              unlink-panels
              :start-placeholder="$t('开始日期')"
              :end-placeholder="$t('结束日期')">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </SearchForm>


    <el-card shadow="never" style="margin-top: 10px">
      <el-row style="margin-bottom: 10px">
        <el-col style="text-align: right">
          <el-button type="primary" @click="batchExemption" v-show="$permiss[175]">{{ $t('批量免审') }}
          </el-button>
          <el-button type="primary" @click="batchToExemption" v-show="$permiss[176]">{{ $t('批量去免审') }}
          </el-button>
        </el-col>
      </el-row>
      <Table max-height="600px" :table-data="tableData" :table-column="tableColumn" :page="page"
             :loading="loading"
             @handleSelectionChange="handleSelectionChange"
             @handleCurrentChange="handleCurrentChange" :selection="true" :select-rows="selectRows"
             @handleSizeChange="handleSizeChange">
        <template slot-scope="scope" slot="avoidAudit">
          <el-switch @change="changeStatus(scope.row)"
                     :value="scope.row.avoidAudit" :disabled="!$permiss[173]"></el-switch>
        </template>
        <template slot-scope="scope" slot="handle">
          <el-button type="text" @click="resetPassword(scope.row)" v-show="$permiss[174]">{{
              $t('重置密码')
            }}
          </el-button>
        </template>
      </Table>
    </el-card>
  </div>
</template>

<script>
import {dateFormatSection, defaultTimeDate} from "@/utils/tools";
import {selectTenant, selectProduct} from "@/api/system";
import {selectCustomerUserList,resetPassword,updateUsers} from '@/api/user'
export default {
  name: 'userList',
  data() {
    return {
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        normalRefundSymbol: '=',
        settlementSymbol: '=',
        timeValue: defaultTimeDate(30)
      },
      symbolList: [
        {label: '>'},
        {label: '='},
        {label: '≧'},
        {label: '≦'},
        {label: '<'}
      ],
      loading:false,
      tenantList: [],
      productList: [],
      tableData: [],
      tableColumn: [
        {label: this.$t('所属租户'), prop: 'adminUserName', isHide: !this.$permiss[171], minWidth: 100},
        {label: this.$t('用户姓名'), prop: 'name', minWidth: 100},
        {label: this.$t('手机号码'), prop: 'phone', minWidth: 100},
        {label: this.$t('身份证号码'), prop: 'idcard', minWidth: 100},
        {label: this.$t('邮箱'), prop: 'mali', minWidth: 100},
        {label: 'whatsAPP', prop: 'whatsApp', minWidth: 100},
        {label: 'zalo', prop: 'zalo', minWidth: 100},
        {label: this.$t('性别'), prop: 'gender', minWidth: 100},
        {label: this.$t('年龄'), prop: 'age', minWidth: 100},
        {label: this.$t('生日'), prop: 'dateBirth', minWidth: 150},
        {label: this.$t('渠道'), prop: 'channel', minWidth: 150},
        {label: this.$t('注册时间'), prop: 'creationTime', minWidth: 150},
        {label: this.$t('APP名称'), prop: 'loanProductName', minWidth: 100},
        {label: this.$t('最大可借金额'), prop: 'canAmount', minWidth: 100},
        {label: this.$t('是否为黑名单'), prop: 'blacklist', minWidth: 100},
        {label: this.$t('是否为白名单'), prop: 'whiteList', minWidth: 100},
        {label: this.$t('结清次数'), prop: 'settlementNum', minWidth: 100},
        {label: this.$t('正常还款率'), prop: 'normalRefundRatio', minWidth: 100},
        {label: this.$t('是否免审核'), minWidth: 100, slot: 'avoidAudit'},
        {label: this.$t('操作'), minWidth: 150, slot: 'handle', isHide: !this.$permiss[174]},
      ],
      selectRows: []
    }

  },
  async mounted() {
    this.getTableData()
    selectTenant().then(res => {
      this.tenantList = res.data
    })
    selectProduct().then(res => {
      this.productList = res.data
    })
  },
  methods: {
    async getTableData() {
      const timeValue = dateFormatSection(this.page.timeValue)
      const normalRefundSymbol =this.page.normalRefundSymbol
      const settlementSymbol = this.page.settlementSymbol
      const params = Object.assign({}, this.page, {
        timeStart: timeValue && timeValue.length ? timeValue[0] : undefined,
        timeEnd: timeValue && timeValue.length ? timeValue[1] : undefined,
        normalRefundSymbol: normalRefundSymbol === '≧' ? '>=' : normalRefundSymbol === '≦' ? '<=' : normalRefundSymbol,
        settlementSymbol: settlementSymbol === '≧' ? '>=' : settlementSymbol === '≦' ? '<=' : settlementSymbol,
      })
      delete params.timeValue
      this.loading = true
      const res = await selectCustomerUserList(params)
      const {list,total} = res.data
      if (res.code === 1) {
        this.tableData = list.map(item => {
          item.avoidAudit = item.avoidAudit === 1
          item.whiteList = item.whiteList === 1 ? this.$t('是') : this.$t('否')
          item.blacklist = item.blacklist === 1 ? this.$t('是') : this.$t('否')
          item.gender = item.gender === '0' ? this.$t('女') : item.gender === '1' ? this.$t('男') : ''
          return item
        })
        this.page = Object.assign({}, this.page, {
          total: total,
        })
        this.loading = false
      } else {
        this.loading = false
      }
    },
    handleSelectionChange(value) {
      this.selectRows = value
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getTableData()
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    // 批量免审
    batchExemption() {
      if (this.selectRows.length === 0) return this.$message.error(this.$t('请先选择!'))
      const rows = []
      this.selectRows.forEach(item => {
        rows.push({
          avoidAudit: 1,
          userId: item.userId
        })
      })
      this.upDataStatus(rows)
    },
    // 批量去免审
    batchToExemption() {
      if (this.selectRows.length === 0) return this.$message.error(this.$t('请先选择!'))
      const rows = []
      this.selectRows.forEach(item => {
        rows.push({
          avoidAudit: 2,
          userId: item.userId
        })
      })
      this.upDataStatus(rows)
    },
    async resetPassword(row) {
      this.$confirm(this.$t('确定重置密码?'), this.$t('提示')).then(async () => {
        const formData = new FormData()
        formData.append('userId', row.userId)
        const res = await resetPassword(formData)
        if (res.code === 1) {
          this.$message.success(this.$t('重置成功'))
          this.getTableData()
        }
      })
    },
    tenantChange(value) {
      selectProduct({adminUserId: value}).then(res => {
        this.productList = res.data
        this.$set(this.page, 'productName', [])
      })
    },
    changeStatus(row) {
      const rowsArr = []
      rowsArr.push({
        avoidAudit: row.avoidAudit ? 2 : 1,
        userId: row.userId
      })
      this.upDataStatus(rowsArr)
    },
    async upDataStatus(rows) {
      const res = await updateUsers(rows)
      if (res.code === 1) {
        this.$message.success(this.$t('修改成功'))
        this.selectRows = []
        this.getTableData()
      }
    },
    resetPage() {
      this.page = {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        normalRefundSymbol: '=',
        settlementSymbol: '=',
      }
      this.getTableData()
    }
  }
}
</script>

<style scoped>

::v-deep .input-with-select .el-input-group__prepend {
  background-color: #fff;
}
</style>
